
var usr = document.getElementById("usr");
var usr_tip = usr.nextElementSibling;
var pass = document.getElementById("password");
var pass_tip = pass.nextElementSibling;
var pass2 = document.getElementById("password2")
var imail = document.getElementById("imail")
var imailnext = imail.nextElementSibling;
var submit = document.getElementById("submit");

     function success(input,tip){
           tip.style.display = "none";
           input.className = "success";
     }
     function error(tip,input,tipEle){
           tipEle.style.display = "block";
           input.className = "";
           tipEle.className = "warn";
           tipEle.innerHTML = tip
     }
     var regList = {
           "usrReg" :  /^[a-z\u2E80-\u9FFF\-_0-9]{4,20}$/i,
           "pureNumber":/^\d+$/i,
           "maxlength4" : /^.{0,4}$/,
           "hasNumber" : /\d/,
           "hasLetter" : /[a-zA-Z]/,
           "hasSC" : /[\!\@\#\$\%\^]/,
           "minlength6" : /^.{0,6}$/,
           "imailReg" :/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@[0-9a-z]{2,9}\.[a-z]{2,6}(\.[a-z]{2,3})?$/i,

     }

     var mandatoryList = [usr,pass,pass2,imail] 
     function validate(str,reg){
           return reg.test(str);
     }
     function validateUsr(){
           var usrValue = usr.value;
           if(validate(usrValue,regList.maxlength4)){
                 return error("请输入5位以上的账号",usr,usr_tip);
           }
           if(validate(usrValue,regList.pureNumber)){
                 return error("用户名不能为纯数字",usr,usr_tip);
           }
           if(validate(usrValue,regList.usrReg)){
                 return success(usr,usr_tip);
           }
           return error("支持中文，英文，数字，字母，-，_ 的4~20位字符",usr,usr_tip);
     }
     function validatePass(){
           var passValue = pass.value;
           var strength = 0;
           if(validate(passValue,regList.hasNumber)){
                 strength++;
           }
           if(validate(passValue,regList.hasLetter)){
                 strength++;
           }
           if(validate(passValue,regList.hasSC)){
                 strength++;
           }

           if(validate(passValue,regList.minlength6)){
                 return error("请输入六位以上的代码",pass,pass_tip)
           }
           if(strength === 3){
                 success(pass,pass_tip)
           }else if(strength === 2){
                 error("密码强度不足，建议升级密码",pass,pass_tip)
           }else if(strength === 1){
                 error("密码强度严重不足，强烈建议升级密码",pass,pass_tip)
           }
     }     
      function validatePassAgain(){
            var passValue = pass.value;
            var passValue2 = pass2.value;
            var pass2_tip=pass2.nextElementSibling;
            if(passValue===passValue2 && passValue2!==""){
                  return success(pass2,pass2_tip)
            }else{
                  pass2_tip.innerHTML='密码错误，请重新输入';
                  pass2_tip.className='error';
            }
      }
      function validateimail(){
            var imailValue = imail.value;
            if(validate(imailValue,regList.imailReg)){
                  return success(imail,imailnext)
            }
            else{
                  return error("请输入正确邮箱",imail,imailnext);
            }
      }
     function submitData(){

           for(var i = 0 , ele ; ele = mandatoryList[i++];  ){
                 if(ele.className.split(" ").indexOf("success") === -1){
                       ele.focus();
                       return ;
                 }
           }
           alert("提交成功");
     }
     usr.addEventListener("blur",validateUsr);
     pass.addEventListener("blur",validatePass);
     pass2.addEventListener("blur",validatePassAgain);
     imail.addEventListener('blur',validateimail);
     submit.addEventListener("click",submitData);

